package jmars.mobile.worker;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import jmars.mobile.common.Investigator;
import jmars.mobile.datainvestigation.Summary;
import jmars.mobile.domain.NumericalFeature;

public class SimpleStat implements Investigator {

	@Override
	public Long compute(File[] files, FileWriter writer) throws IOException {
		Long total = 0L;
		Long line;
		FileInputStream fis;
		BufferedReader reader;
		String row;
		String[] cols;
		Map<String,NumericalFeature> stat=new HashMap<String,NumericalFeature>();
		NumericalFeature rowStat;
		DecimalFormat df = new DecimalFormat();
		df.applyPattern("0,000");
		Double temp;
		for(File inputFile:files){
			fis = new FileInputStream(inputFile);
			reader = new BufferedReader(new InputStreamReader(fis));
			line=0L;
			while((row=reader.readLine())!=null){
				line++;
				cols=row.split(Summary.columnSeperator);
				if(Summary.filterRow(cols,false,true)) continue;
				for(Integer idx:Summary.targetColumnIdx){
					if(idx<cols.length){
						try{
							if(stat.containsKey(Summary.columns[idx])) rowStat=stat.get(Summary.columns[idx]);
							else{
								rowStat = new NumericalFeature();
							}
							temp = Double.parseDouble(cols[idx]);
							rowStat.update(temp);
							stat.put(Summary.columns[idx], rowStat);
						}catch(Exception ex){
							System.out.println("exception at line "+line+" of "+inputFile.getName());
							System.out.println("data: "+cols[idx]);
							System.out.println(row);
							if(!Summary.errorResume){
								ex.printStackTrace();
								System.exit(0);
							}
						}
					}
				}
				total++;
			}
			reader.close();
			fis.close();
			System.out.println(inputFile.getName()+" finished at "+total);
		}
		writer.write("%total:"+df.format(total)+"\r\n");
		if(stat.size()<=1000){
			writer.write("%*****************details******************\r\n");
			for(String s:stat.keySet()){
				writer.write(s.replaceAll("\\$_\\$\\$_\\$", ",").replaceAll("\\$_\\$","")+": "+stat.get(s)+"\r\n");
			}
		}
		return total;
	}

}
